/* 
 * 定时器 
 */

#include <linux/init.h>
#include <linux/printk.h>
#include <linux/module.h>
#include <linux/timer.h>

#define BLINK_DELAY HZ / 2
static struct timer_list my_timer;
static int count = 0;
static void my_timer_func(struct timer_list *unused)
{
    pr_info("callback timer, count=%d\n",count++);
	my_timer.expires = jiffies + BLINK_DELAY;
	add_timer(&my_timer);
}

static int __init timer_test_init(void)
{
	timer_setup(&my_timer, my_timer_func, 0);
	my_timer.expires = jiffies + BLINK_DELAY;
	add_timer(&my_timer);

	return 0;
}

static void __exit timer_test_exit(void)
{
	pr_info("timer: unloading...\n");
	del_timer(&my_timer);
}

module_init(timer_test_init);
module_exit(timer_test_exit);

MODULE_LICENSE("GPL");

/* 结果: 每0.5s运行一次 my_timer_func

Dec 30 19:48:36 ubuntu kernel: [ 3529.961320] callback timer, count=0
Dec 30 19:48:37 ubuntu kernel: [ 3530.477419] callback timer, count=1
Dec 30 19:48:37 ubuntu kernel: [ 3530.985305] callback timer, count=2
Dec 30 19:48:38 ubuntu kernel: [ 3531.497375] callback timer, count=3
...
Dec 30 19:48:40 ubuntu kernel: [ 3533.753201] timer: unloading...
 */
